package com.example.socialmediaplatform.controller;

import com.example.socialmediaplatform.model.Message;
import com.example.socialmediaplatform.model.MessageRepository;
import com.example.socialmediaplatform.model.Notification;
import com.example.socialmediaplatform.model.NotificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/message")
public class MessageController {
    @Autowired
    private MessageRepository messageRepository;

    @Autowired
    private NotificationRepository notificationRepository;

    @Autowired
    private MessageWebSocketController messageWebSocketController;

    // 发送私信
    @PostMapping("/send")
    public ResponseEntity<?> sendMessage(Authentication authentication, @RequestParam String toUser, @RequestParam String content) {
        Message msg = new Message();
        msg.setFromUser(authentication.getName());
        msg.setToUser(toUser);
        msg.setContent(content);
        msg.setReaded(false);
        msg.setCreatedAt(new Date());
        messageRepository.save(msg);
        
        // 创建通知
        Notification notification = new Notification();
        notification.setType("newMessage");
        notification.setContent(authentication.getName() + " 给你发送了一条私信");
        notification.setFromUser(authentication.getName());
        notification.setToUser(toUser);
        notification.setReaded(false);
        notification.setCreatedAt(new Date());
        notificationRepository.save(notification);
        
        // WebSocket推送 - 暂时注释掉，使用新的STOMP方式
        // messageWebSocketController.sendMessageToUser(toUser, msg);
        return ResponseEntity.ok("发送成功");
    }

    // 查询与某用户的历史消息
    @GetMapping("/history/{otherUser}")
    public ResponseEntity<?> history(Authentication authentication, @PathVariable String otherUser) {
        List<Message> sent = messageRepository.findByFromUserAndToUserOrderByCreatedAtAsc(authentication.getName(), otherUser);
        List<Message> received = messageRepository.findByFromUserAndToUserOrderByCreatedAtAsc(otherUser, authentication.getName());
        sent.addAll(received);
        sent.sort((a, b) -> a.getCreatedAt().compareTo(b.getCreatedAt()));
        return ResponseEntity.ok(sent);
    }

    // 查询未读消息
    @GetMapping("/unread")
    public ResponseEntity<?> unread(Authentication authentication) {
        List<Message> unread = messageRepository.findByToUserAndReadedFalseOrderByCreatedAtDesc(authentication.getName());
        return ResponseEntity.ok(unread);
    }

    // 标记消息为已读
    @PostMapping("/{id}/read")
    public ResponseEntity<?> markRead(Authentication authentication, @PathVariable Long id) {
        Optional<Message> opt = messageRepository.findById(id);
        if (opt.isEmpty()) return ResponseEntity.notFound().build();
        Message m = opt.get();
        if (!m.getToUser().equals(authentication.getName())) return ResponseEntity.status(403).body("无权操作");
        m.setReaded(true);
        messageRepository.save(m);
        return ResponseEntity.ok("已标记为已读");
    }
} 
